package com.crm.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.transaction.annotation.Transactional;

import com.crm.dao.CustomerDao;
import com.crm.domain.Customer;
import com.crm.domain.PageBean;
import com.crm.service.CustomerService;

/**
 * @author Yusei
 * 注意，要添加事务注解带class的上面
 */

@Transactional
public class CustomerServiceImpl implements CustomerService {
	// 注入dao
	private CustomerDao customerDao;
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	
	@Override
	// 保存客户信息到数据库
	public void save(Customer customer) {
		customerDao.save(customer);
	}

	@Override
	// 分页查询
	public PageBean<Customer> findByPage(DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) {
		// 给pageBean赋值
		PageBean<Customer> pageBean = new PageBean<Customer>();
		pageBean.setCurrPage(currPage);
		pageBean.setPageSize(pageSize);
		// 总记录数（查询数据库得到）
		Integer totalCount = customerDao.findCount(detachedCriteria);
		pageBean.setTotalCount(totalCount);
		// 通过计算得到总页数
		Double tc = totalCount.doubleValue();
		// 向下取整
		Double num = Math.ceil(tc/pageSize);
		pageBean.setTotalPage(num.intValue());
		// 封装每页显示的数据的集合
		Integer begin = ( currPage - 1 ) * pageSize;
		List<Customer> list = customerDao.findByPage(detachedCriteria, begin, pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	// 根据id查询用户
	public Customer findById(long cust_id) {
		return customerDao.findById(cust_id);
	}

	@Override
	// 删除数据库中的信息
	public void delete(Customer customer) {
		customerDao.delete(customer);
		
	}

	@Override
	// 修改客户信息
	public void update(Customer customer) {
		customerDao.update(customer);
	}

	@Override
	// 查询所有客户
	public List<Customer> findAll() {
		return customerDao.findAll();
	}
	
	
}
